package com.purchase.ml.resource.prod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.purchase.ml.annotations.OperLog;
import com.purchase.ml.bean.base.ApiRestResult;
import com.purchase.ml.bean.req.dealer.DealerCategoryEditDto;
import com.purchase.ml.bean.req.dealer.DealerCategoryQueryDto;
import com.purchase.ml.bean.req.dealer.SysDealerNewDto;
import com.purchase.ml.bean.req.prod.SysCategoryEditDto;
import com.purchase.ml.bean.req.prod.SysCategoryNewDto;
import com.purchase.ml.bean.req.prod.SysCategoryQueryDto;
import com.purchase.ml.consts.PermissionsConst;
import com.purchase.ml.entity.prod.SysCategory;
import com.purchase.ml.exception.BizException;
import com.purchase.ml.exception.CommonEnum;
import com.purchase.ml.service.prod.SysCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Locale;

/**
 * @author :zxm
 * @date :2021/2/25 18:41
 * @description :some description
 **/
@RestController
@RequestMapping("/category")
@Slf4j
@Api(tags = "商品分类")
public class CategoryResource {

    @Autowired
    private SysCategoryService categoryService;

    @PostMapping("/new")
    @ApiOperation("新增产品分类")
    @RequiresPermissions(PermissionsConst.CATGORY_NEW)
    @OperLog(operDesc = "CreateCategory", operModul = "Category", operType = "Create")
    public ApiRestResult createCategory(@RequestBody @Valid SysCategoryNewDto categoryNewDto){
        ApiRestResult result = categoryService.saveCategory(categoryNewDto);
        return result;
    }

    @PostMapping("/edit")
    @ApiOperation("修改产品分类")
    @RequiresPermissions(PermissionsConst.CATGORY_EDIT)
    @OperLog(operDesc = "EditCategory", operModul = "Category", operType = "Edit")
    public ApiRestResult editCategory(@RequestBody @Valid SysCategoryEditDto categoryEditDto){
        ApiRestResult result = categoryService.editCategory(categoryEditDto);
        return result;
    }

    @PostMapping("/query")
    @ApiOperation("分页查询产品分类")
    @RequiresPermissions(PermissionsConst.CATGORY_QUERY)
    public ApiRestResult categoryPage(@RequestBody SysCategoryQueryDto categoryQueryDto){
        IPage<SysCategory> result = categoryService.categoryQuery(categoryQueryDto);
        return ApiRestResult.ok(result);
    }

    @PostMapping("/queryShop")
    @ApiOperation("分页查询商店产品目录")
    public ApiRestResult shopCategoryPage(@RequestBody SysCategoryQueryDto dto){
        IPage<SysCategory> resultPage = categoryService.shopCategoryPage(dto);
        return ApiRestResult.ok(resultPage);
    }

    @PostMapping("/delete")
    @ApiOperation("删除产品目录")
    @OperLog(operDesc = "DeleteCategory", operModul = "Category", operType = "Delete")
    public ApiRestResult deleteCategory(@RequestBody List<Long> ids){
        categoryService.batchDeleteCategory(ids);
        return ApiRestResult.ok();
    }
}
